<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:relevancy:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      stripe
      border
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
    >
      <el-table-column label="租户名称" align="center" prop="tenantName" />
      <el-table-column label="计量企业名称" align="center" prop="meteringName" />
      <el-table-column label="商户ID" align="center" prop="merchantId" />
      <el-table-column label="商户秘钥" align="center" prop="secretKey" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:relevancy:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id,scope.row.tenantId)"
            v-hasPermi="['system:relevancy:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RelevancyForm ref="formRef" @success="getList" />
</template>
<script setup lang="tsx" name="SystemRelevancy">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import RelevancyForm from './RelevancyForm.vue'
import * as RelevancyApi from '@/api/relevancy'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,

})

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询关联企业列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RelevancyApi.getRelateEnterInfo(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
const queryData = reactive({
  tenantId: undefined
})

/** 删除按钮操作 */
const handleDelete = async (id?: number,tenantId?: number) => {
  queryData.tenantId = tenantId
  try {
    const res = await RelevancyApi.checkUnprocessedSections(queryData)
      if (res > 0){
        // 删除的二次确认
        await message.delConfirm("当前企业还存在审核通过且履约未处理的标段，确认要删除吗？")
      }else {
        // 删除的二次确认
        await message.delConfirm("确定要删除数据吗?")
      }
      // 发起删除
      await RelevancyApi.deleteRelevancy(id)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
